package com.siyao.redvip.components.base;

import com.google.gson.Gson;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;
import com.siyao.redvip.R;
import com.siyao.redvip.RedApplication;
import com.siyao.redvip.bean.UserBean;
import com.siyao.redvip.components.view.LoadingDialog;
import com.siyao.redvip.ui.activity.RedvipMainActivity;
import com.siyao.redvip.utils.Track;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

/**
 * BaseFragmentActivity
 * @author wangxuebin
 *
 */
public abstract class BaseFragmentActivity extends FragmentActivity implements OnClickListener{
	
	public LayoutParams layoutParams;
	
	protected ImageLoader imageLoader = ImageLoader.getInstance();
	
	protected RelativeLayout appTopView, appMainView;
	
	public LoadingDialog pd;
    protected Gson gson = new Gson();
	
    protected  String account ="";
    
    protected RedApplication redApplication;
	protected UserBean user;
	
	 public DisplayImageOptions options;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		this.setContentView(R.layout.app_base_layout);
		appTopView = (RelativeLayout) this.findViewById(R.id.app_layout_top);
	    appMainView = (RelativeLayout) this.findViewById(R.id.app_layout_main);
	    layoutParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
	    options = new DisplayImageOptions.Builder().showImageOnLoading(R.drawable.ic_loading).showImageForEmptyUri(R.drawable.ic_load_faild)
                .showImageOnFail(R.drawable.ic_load_faild).cacheInMemory(true).cacheOnDisc(true).considerExifParams(true)
                .displayer(new RoundedBitmapDisplayer(20)).build();
	    
	    pd = new LoadingDialog(this);
        pd.setCancelable(false);
        redApplication =RedApplication.getInstance();
        beforeInit();
        ininlazeLayout();
        ininlazeView();
        ininlazeData();
	}
	
	

	@Override
    protected void onStop() {
        super.onStop();
        dismissPd();
    }
	
	  protected void showPd() {
	        try {
	            if (pd != null && !pd.isShowing()) {
	                pd.show();
	            }
	        } catch (Exception e) {
	            e.printStackTrace();
	        }
	    }
	  
	protected void dismissPd() {
        try {
            if (pd != null && pd.isShowing()) {
                pd.dismiss();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
	
	protected void setTitleText(String titleText) {
        if (null == appTopView) {
            return;
        }
        if (null == titleText || "".equals(titleText)) {
            return;
        }
        TextView titleTextView = (TextView) findViewById(R.id.top_center_tv);
        titleTextView.setText(titleText);

        if (appTopView.getVisibility() != View.VISIBLE) {
            appTopView.setVisibility(View.VISIBLE);
        }

        titleTextView.setVisibility(View.VISIBLE);
    }
	
	protected void  setLeftButton(String text){
		if(appTopView ==null){
			return;
		}
		 if (null == text || "".equals(text)) {
	            return;
	        }
		Button leftButton = (Button) findViewById(R.id.top_left_btn);
		leftButton.setOnClickListener(this);
		leftButton.setText(text);
		if (appTopView.getVisibility() != View.VISIBLE) {
            appTopView.setVisibility(View.VISIBLE);
        }
		leftButton.setVisibility(View.VISIBLE);
	}
	
	protected void  setRightButton(int  resourceId){
		if(appTopView ==null){
			return;
		}
		Button rightButton = (Button) findViewById(R.id.top_right_btn_image);
		rightButton.setOnClickListener(this);
		rightButton.setBackgroundResource(resourceId);
		if (appTopView.getVisibility() != View.VISIBLE) {
            appTopView.setVisibility(View.VISIBLE);
        }
		rightButton.setVisibility(View.VISIBLE);
	}
	
	/**
	 * 加载数据之前
	 */
	protected abstract void beforeInit(); 
	/**
	 * 加载控件
	 */
	public abstract void ininlazeView();
	
	/**
	 * 加载数据
	 */
	public abstract void ininlazeData();
	
	/**
	 * 加载layoutId
	 * @param layoutId
	 */
	public abstract void ininlazeLayout();
	
	
	public void jumpActivity(Intent intent){
		startActivity(intent);
	}
	
	@Override
	public void onClick(View v) {
		int viewId=v.getId();
		switch (viewId) {
		case R.id.top_left_btn:
			Intent intent = new Intent(this, RedvipMainActivity.class);
			startActivity(intent);
			break;

		default:
			break;
		}
		
	}
}
